package com.jinxin.platform.cdacsfacetalk.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

@Slf4j
public class Utils {

    public static HashMap transformBean2Map(Object obj) {
        HashMap map = new HashMap<>();
        if (obj == null) {
            return map;
        }
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);
                    if(!StringUtils.isEmpty(value)){
                        map.put(key, String.valueOf(value));
                    }
                }

            }
        } catch (Exception e) {
            log.error("transformBean2Map Error {0}" ,e);
        }
        return map;
    }

    public static String getUuid() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }
    /**
     * 获取位数为7位的随机数
     * @return
     * */
    public static int getRandom() {
        Random random = new Random();
        int nextInt = random.nextInt(9000000);
        nextInt=nextInt+1000000;
        return nextInt;
    }

}
